﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace SenetExperimentsRunner.Utils
{
    public class MyCustomTypeDescriptor : CustomTypeDescriptor
    {
        private string[] m_PropertyToExpand = null;
        private string m_PropertyPrefix = null;
        private bool m_ShowPropertyParent = false;

        public MyCustomTypeDescriptor(
            ICustomTypeDescriptor parent,
            string[] propertyToExpand,
            string propertyPrefix,
            bool showPropertyParent)
            : base(parent)
        {
            m_PropertyToExpand = propertyToExpand;
            m_PropertyPrefix = propertyPrefix;
            m_ShowPropertyParent = showPropertyParent;
        }

        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection baseProperties = base.GetProperties(attributes);
            return GetPropertiesAux(baseProperties);
        }

        public override PropertyDescriptorCollection GetProperties()
        {
            PropertyDescriptorCollection baseProperties = base.GetProperties();
            return GetPropertiesAux(baseProperties);
        }

        private List<PropertyDescriptor> GetPropertiesAux(
            PropertyDescriptorCollection baseProperties,
            string propertyName)
        {
            string propertyPrefix = (m_PropertyPrefix ?? String.Empty);
            string inFix = propertyName + ".";
                //(m_ShowPropertyParent ? (propertyName + "\n") : String.Empty);

            // get the property descriptor of propertyName
            PropertyDescriptor propertyDescriptor = baseProperties[propertyName];

            // get the children of that property
            PropertyDescriptorCollection childProperties = propertyDescriptor.GetChildProperties();
            List<PropertyDescriptor> propertyDescriptors = new List<PropertyDescriptor>();

            foreach (PropertyDescriptor childProperty in childProperties)
            {
                GeneralSubPropertyDescriptor generalSubPropertyDescriptor =
                    new GeneralSubPropertyDescriptor(
                        propertyDescriptor,
                        childProperty,
                        propertyPrefix + inFix + childProperty.Name);
                propertyDescriptors.Add(generalSubPropertyDescriptor);
            }
            return propertyDescriptors;
        }

        private PropertyDescriptorCollection GetPropertiesAux(PropertyDescriptorCollection baseProperties)
        {
            List<PropertyDescriptor> propertyDescriptors = new List<PropertyDescriptor>();

            // add the direct properties of the parent
            // we only add properties that should not be expanded
            foreach (PropertyDescriptor property in baseProperties)
            {
                bool addProperty = true;
                foreach (string propertyName in m_PropertyToExpand)
                {
                    if (property.Name.Equals(propertyName))
                    {
                        addProperty = false;
                        break;
                    }
                }
                if (addProperty)
                {
                    propertyDescriptors.Add(property);                 
                }

            }

            // add the properties that were marked to expand
            foreach (string propertyToExpand in m_PropertyToExpand)
            {
                propertyDescriptors.AddRange(GetPropertiesAux(baseProperties, propertyToExpand));
            }
            return new PropertyDescriptorCollection(propertyDescriptors.ToArray());
        }
    }

}
